package com.qianniu.napi.common.util;

import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Positions;

import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class Thumbnailator {



    /**
     *
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {

        Thumbnailator thumbnailatorTest = new Thumbnailator();

        thumbnailatorTest.test1();
//        thumbnailatorTest.test2();
//        thumbnailatorTest.test3();
//        thumbnailatorTest.test4();
//        thumbnailatorTest.test5();
//        thumbnailatorTest.test6();
//        thumbnailatorTest.test7();
//        thumbnailatorTest.test8();
//        thumbnailatorTest.test9();
    }

    /**
     * 指定大小进行缩放
     *
     * @throws IOException
     */
    private void test1() throws IOException {
        /*
         * size(width,height) 若图片横比200小，高比300小，不变
         * 若图片横比200小，高比300大，高缩小到300，图片比例不变 若图片横比200大，高比300小，横缩小到200，图片比例不变
         * 若图片横比200大，高比300大，图片按比例缩小，横为200或高为300
         */
//        Thumbnails.of("images/test.jpg").size(200, 300).toFile("C:/image_200x300.jpg");
//        Thumbnails.of("images/test.jpg").size(2560, 2048).toFile("C:/image_2560x2048.jpg");
//        Thumbnails.of("/Users/chenxuebin/Desktop/yuheng.jpg").size(100, 100).toFile("/Users/chenxuebin/Desktop/yuheng50.jpg");
//        Thumbnails.of("/Users/chenxuebin/Desktop/sharewithlli.png").size(375, 555).toFile("/Users/chenxuebin/Desktop/sharewithlli375.png");
//        Thumbnails.of("https://img.niucaiduo.com/gyqqq/store/tmp_fccf00aa2a5f.jpg").size(375, 555).toFile("/Users/chenxuebin/Desktop/tmp_fccf00aa2a5f.jpg");
//        thumbnail("/Users/chenxuebin/Pictures/niucaiduo_for_weixin_open.png",0.5f);


//        Thumbnails.of("/Users/chenxuebin/Pictures/qianniu_quanx108.png").size(28, 28).toFile("/Users/chenxuebin/Pictures/qianniu_quanx108.png");
//        Thumbnails.of("/Users/chenxuebin/Downloads/tmp_img/ncd200.png").size(100, 100).toFile("/Users/chenxuebin/Downloads/tmp_img/ncd100.png");
//        Thumbnails.of("/Users/chenxuebin/SVN/qnmapp/ncd/img/canyu.png").size(60, 60).toFile("/Users/chenxuebin/SVN/qnmapp/ncd/img/canyu2.png");
//        Thumbnails.of("/Users/chenxuebin/SVN/qnmapp/ncd/img/mdxx.png").size(370,65).outputQuality(0.6).toFile("/Users/chenxuebin/SVN/qnmapp/ncd/img/mdxx2.png");
//        Thumbnails.of("/Users/chenxuebin/Downloads/tmp_img/qianniu_logo2020.jpg").size(108,108)
//                .outputFormat("png").toFile("/Users/chenxuebin/Downloads/tmp_img/qianniu_logo_new108.png");

        Thumbnails.of("/Users/chenxuebin/Desktop/O1CN01ptRnZM1bWDh72.jpg").size(750, 750)
                .toFile("/Users/chenxuebin/Desktop/O1CN01ptRnZM1bWDh722.jpg");
    }

    /**
     * 按照比例进行缩放
     *
     * @throws IOException
     */
    private void test2() throws IOException {
        /**
         * scale(比例)
         */
//        Thumbnails.of("/Users/chenxuebin/Downloads/tmp_img/meikeduoduo_img/3.jpg").scale(0.1f).toFile("/Users/chenxuebin/Downloads/tmp_img/meikeduoduo_img/33.jpg");
//        Thumbnails.of("/Users/chenxuebin/Downloads/tmp_img/meikeduoduo_img/4.jpg").scale(0.1f).toFile("/Users/chenxuebin/Downloads/tmp_img/meikeduoduo_img/44.jpg");
//        Thumbnails.of("/Users/chenxuebin/Downloads/tmp_img/meikeduoduo_img/5.jpg").scale(0.1f).toFile("/Users/chenxuebin/Downloads/tmp_img/meikeduoduo_img/55.jpg");
//        Thumbnails.of("/Users/chenxuebin/Downloads/tmp_img/meikeduoduo_img/6.jpg").scale(0.1f).toFile("/Users/chenxuebin/Downloads/tmp_img/meikeduoduo_img/66.jpg");
//        Thumbnails.of("/Users/chenxuebin/Downloads/tmp_img/meikeduoduo_img/7.jpg").scale(0.1f).toFile("/Users/chenxuebin/Downloads/tmp_img/meikeduoduo_img/77.jpg");
//        Thumbnails.of("/Users/chenxuebin/Downloads/tmp_img/meikeduoduo_img/8.jpg").scale(0.1f).toFile("/Users/chenxuebin/Downloads/tmp_img/meikeduoduo_img/88.jpg");
//        Thumbnails.of("/Users/chenxuebin/Downloads/tmp_img/meikeduoduo_img/9.jpg").scale(0.1f).toFile("/Users/chenxuebin/Downloads/tmp_img/meikeduoduo_img/99.jpg");

//        thumbnail("/Users/chenxuebin/Downloads/tmp_img/meikeduoduo_img/3.jpg",0.1f);
//        thumbnail("/Users/chenxuebin/Downloads/tmp_img/meikeduoduo_img/4.jpg",0.1f);
//        thumbnail("/Users/chenxuebin/Downloads/tmp_img/meikeduoduo_img/5.jpg",0.1f);

//        thumbnail("/Users/chenxuebin/Downloads/tmp_img/meikeduoduo_img/6.jpg",0.1f);
//        thumbnail("/Users/chenxuebin/Downloads/tmp_img/meikeduoduo_img/7.jpg",0.1f);
//        thumbnail("/Users/chenxuebin/Downloads/tmp_img/meikeduoduo_img/8.jpg",0.1f);
//        thumbnail("/Users/chenxuebin/Downloads/tmp_img/meikeduoduo_img/9.jpg",0.1f);

//        thumbnail("/Users/chenxuebin/Desktop/tmp_fccf00aa2a5f.jpg",0.25f);
//        thumbnail("/Users/chenxuebin/Desktop/tmp_36be2e1389bf.jpg",0.5f);
//        thumbnail("/Users/chenxuebin/Desktop/tmp_d60bae9606be.jpg",0.25f);

//        thumbnail("/Users/chenxuebin/Desktop/tmp_e0306456ccb5.jpg",0.25f);
//        thumbnail("/Users/chenxuebin/Desktop/tmp_2fce8c942ec4.jpg",0.25f);
//        thumbnail("/Users/chenxuebin/Desktop/tmp_f42e35297540.jpg",0.25f);
//        thumbnail("/Users/chenxuebin/Desktop/tmp_cd4b2f011381.jpg",0.25f);
//        thumbnail("/Users/chenxuebin/Desktop/tmp_dd06b68c065d.jpg",0.25f);
//        thumbnail("/Users/chenxuebin/Desktop/tmp_29003dfe39a1.jpg",0.25f);
//        thumbnail("/Users/chenxuebin/Desktop/tmp_b544140efcb6.jpg",0.25f);

//        thumbnail("/Users/chenxuebin/Pictures/website_for_weixin.png",0.5f);
        thumbnail("/Users/chenxuebin/Pictures/niucaiduo_for_weixin_open.png",0.5f);

    }

    private void thumbnail(String filePathAndName, double size) {
        try {
            Thumbnails.of(filePathAndName).scale(size).toFile(filePathAndName);
        } catch (IOException e) {
            File cmykJPEGFile = new File(filePathAndName);
            try {
                BufferedImage image = ImageIO.read(cmykJPEGFile);
                ImageOutputStream output = ImageIO.createImageOutputStream(cmykJPEGFile);
                if (!ImageIO.write(image, "jpg", output)) {
                }
                Thumbnails.of(image).scale(size).toFile(filePathAndName);
            } catch (IOException e1) {
            }
        }
    }

    /**
     * 不按照比例，指定大小进行缩放
     *
     * @throws IOException
     */
    private void test3() throws IOException {
        /**
         * keepAspectRatio(false) 默认是按照比例缩放的
         */
        Thumbnails.of("images/test.jpg").size(120, 120).keepAspectRatio(false).toFile("C:/image_120x120.jpg");
    }

    /**
     * 旋转
     *
     * @throws IOException
     */
    private void test4() throws IOException {
        /**
         * rotate(角度),正数：顺时针 负数：逆时针
         */
        Thumbnails.of("images/test.jpg").size(1280, 1024).rotate(90).toFile("C:/image+90.jpg");
        Thumbnails.of("images/test.jpg").size(1280, 1024).rotate(-90).toFile("C:/iamge-90.jpg");
    }

    /**
     * 水印
     *
     * @throws IOException
     */
    private void test5() throws IOException {
        /**
         * watermark(位置，水印图，透明度)
         */
        Thumbnails.of("images/test.jpg").size(1280, 1024).watermark(Positions.BOTTOM_RIGHT, ImageIO.read(new File("images/watermark.png")), 0.5f)
                .outputQuality(0.8f).toFile("C:/image_watermark_bottom_right.jpg");
        Thumbnails.of("images/test.jpg").size(1280, 1024).watermark(Positions.CENTER, ImageIO.read(new File("images/watermark.png")), 0.5f)
                .outputQuality(0.8f).toFile("C:/image_watermark_center.jpg");
    }

    /**
     * 裁剪
     *
     * @throws IOException
     */
    private void test6() throws IOException {
        /**
         * 图片中心400*400的区域
         */
        Thumbnails.of("images/test.jpg").sourceRegion(Positions.CENTER, 400, 400).size(200, 200).keepAspectRatio(false)
                .toFile("C:/image_region_center.jpg");
        /**
         * 图片右下400*400的区域
         */
        Thumbnails.of("images/test.jpg").sourceRegion(Positions.BOTTOM_RIGHT, 400, 400).size(200, 200).keepAspectRatio(false)
                .toFile("C:/image_region_bootom_right.jpg");
        /**
         * 指定坐标
         */
        Thumbnails.of("images/test.jpg").sourceRegion(600, 500, 400, 400).size(200, 200).keepAspectRatio(false).toFile("C:/image_region_coord.jpg");
    }

    /**
     * 转化图像格式
     *
     * @throws IOException
     */
    private void test7() throws IOException {
        /**
         * outputFormat(图像格式)
         */
        Thumbnails.of("images/test.jpg").size(1280, 1024).outputFormat("png").toFile("C:/image_1280x1024.png");
        Thumbnails.of("images/test.jpg").size(1280, 1024).outputFormat("gif").toFile("C:/image_1280x1024.gif");
    }

    /**
     * 输出到OutputStream
     *
     * @throws IOException
     */
    private void test8() throws IOException {
        /**
         * toOutputStream(流对象)
         */
        OutputStream os = new FileOutputStream("C:/image_1280x1024_OutputStream.png");
        Thumbnails.of("images/test.jpg").size(1280, 1024).toOutputStream(os);
    }

    /**
     * 输出到BufferedImage
     *
     * @throws IOException
     */
    private void test9() throws IOException {
        /**
         * asBufferedImage() 返回BufferedImage
         */
        BufferedImage thumbnail = Thumbnails.of("images/test.jpg").size(1280, 1024).asBufferedImage();
        ImageIO.write(thumbnail, "jpg", new File("C:/image_1280x1024_BufferedImage.jpg"));
    }
}
